package com.easycarebn.main.fragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easycarebn.main.adapter.FragmentAdapter;
import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.PagerSlidingTabStrip;
import com.easycarebn.main.utils.Utils;
import com.timbacsi.main.R;

public class FragmentTab extends Fragment {
	public static int keyMenu;
	public static ViewPager pager;

	public static int index = 0;
	private View view;
	private List<String> listTile;
	private PagerSlidingTabStrip tabs;
	public static Item item;
	public static FragmentTab f = null;
	private IResult callIResult;
	private static String mTitle;

	public static FragmentTab newInstance(int keyMenu, String title) {
		FragmentTab.keyMenu = keyMenu;
		mTitle=title;
		f = new FragmentTab();

		return f;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof IResult)
			callIResult = (IResult) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_tab, container, false);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		callIResult.onDisplay(Utils.TASK_CHILD, "");
		Utils.setTitlePage(view,mTitle );
		listTile = new ArrayList<String>();
		switch (keyMenu) {
		case R.id.menu_yeuthich:
			addListTile(R.array.yeuthich);
			break;

		case R.id.menu_qlhk:
			addListTile(R.array.quanlyhenkham);
			break;

		}

		addView();

		super.onActivityCreated(savedInstanceState);
	}

	public void addListTile(int menu) {
		listTile.addAll(Arrays.asList(getActivity().getResources()
				.getStringArray(menu)));
	}

	public void addView() {
		try {
			tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);

			pager = (ViewPager) view.findViewById(R.id.pagerdtdd);

			FragmentAdapter adapter = new FragmentAdapter(getActivity()
					.getSupportFragmentManager(), listTile, keyMenu, item);
			pager.setAdapter(adapter);

			final int pageMargin = (int) TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, 1, getActivity()
							.getResources().getDisplayMetrics());
			pager.setPageMargin(pageMargin);
			tabs.setViewPager(pager);
			pager.setCurrentItem(index);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}